﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Securfid_WEB.Models;

namespace Securfid_WEB.Controllers
{
    public class HomeController : Controller
    {
        private DataClassesDataContext _db = new DataClassesDataContext();

        [Authorize]
        public ActionResult Index()
        {
            ViewBag.listAppareils = _db.appareils.ToList();
            return View(_db.rfid.ToList());
        }

        [Authorize]
        public ActionResult Create()
        {
            ViewBag.listAppareil = _db.appareils.ToList();
            return View();
        }

        [HttpPost]
        public ActionResult Create(rfid collection)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    //Valeurs par défaut
                    collection.time = new DateTime(1900, 1, 1);
                    collection.heure_alarme = new DateTime(1900, 1, 1);
                    collection.alarme = false;

                    var dataContext = new DataClassesDataContext();
                    dataContext.rfid.InsertOnSubmit(collection);
                    dataContext.SubmitChanges();

                    return RedirectToAction("Index");
                }
                catch
                {
                    return View();
                }
            }
            return View(collection);
        }

        [Authorize]
        public ActionResult Edit(int id)
        {
            var dataContext = new DataClassesDataContext();
            var originalRFID = dataContext.rfid.SingleOrDefault(x => x.id_cegep == id);

            ViewBag.listAppareil = _db.appareils.ToList();

            return View(originalRFID);
        }

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            var dataContext = new DataClassesDataContext();
            var originalRFID = dataContext.rfid.SingleOrDefault(x => x.id_cegep == id); 

            try
            {
                UpdateModel(originalRFID);
                dataContext.SubmitChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                return View(originalRFID);
            }
        }

        [Authorize]
        public ActionResult Delete(int id)
        {
            var dataContext = new DataClassesDataContext();
            var toDeleteRFID = dataContext.rfid.SingleOrDefault(x => x.id_cegep == id);

            // Show Index view 

            return View(toDeleteRFID); 
        }

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            var dataContext = new DataClassesDataContext();
            var toDeleteRFID = dataContext.rfid.SingleOrDefault(x => x.id_cegep == id);
            try
            {
                dataContext.rfid.DeleteOnSubmit(toDeleteRFID);
                dataContext.SubmitChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
